package cn.ichiva.luckysheet.utils;

import com.alibaba.fastjson.JSONArray;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ObjectUtils {
    private ObjectUtils(){}
    public static boolean isEmpty(Object obj) {
        if (obj == null) return true;
        else if (obj instanceof CharSequence) return ((CharSequence) obj).length() == 0;
        else if (obj instanceof Collection) return ((Collection) obj).isEmpty();
        else if (obj instanceof Map) return ((Map) obj).isEmpty();
        else if (obj.getClass().isArray()) return Array.getLength(obj) == 0;
        else if (obj instanceof String) return "".equals(obj);
        return false;
    }
    public static boolean isNotEmpty(Object obj) {
       return !isEmpty(obj);
    }

    public static String trim(String param) {
        String source = null;
        if (null != param) {
            source = param.trim();
            if (!"".equals(source)) {
                return source;
            }
        }
        return null;
    }

    public static String getObject2String(Object obj){
        String result  = null;
        if (obj == null){
            result = "";
        }else{
            result = String.valueOf(obj);
        }
           return result;
    }

    /**
     * Object转long类型
     *
     * @param obj
     * @return Long
     */
    public static Long getObject2Long(Object obj){
        Long result  = null;
        if (obj != null){
            result = Long.valueOf(String.valueOf(obj));
        }
        return result;
    }

    /**
     * Object转BigDecimal类型
     *
     * @param obj
     * @return BigDecimal
     */
    public static BigDecimal getObject2BigDecimal(Object obj){
        BigDecimal result  = null;
        if (obj != null){
            result = new BigDecimal(String.valueOf(obj));
        }
        return result;
    }

    /**
     * Object转Integer类型
     *
     * @param obj
     * @return Integer
     */
    public static Integer getObject2Integer(Object obj){
        Integer result  = null;
        if (obj != null){
            result = Integer.valueOf(String.valueOf(obj));
        }
        return result;
    }

    /**
     * Object转Date类型
     *
     * @param obj
     * @return Date
     */
    public static Date getObject2Date(Object obj){
        Date result  = null;
        if (obj != null){
            if(obj instanceof Date){
                result = (Date)obj;
            } else {
                Long objLong = getObject2Long(obj);
                result = isNotEmpty(objLong)?new Date(objLong):null;
            }
        }
        return result;
    }

    /**
     * Object转List类型
     *
     * @param obj
     * @return List
     */
    public static List getObject2List(Object obj){
        List result  = null;
        if (obj != null && obj instanceof List){
            result = (List)obj;
        }
        return result;
    }

    /**
     * Object转Map类型
     *
     * @param obj
     * @return Map
     */
    public static Map getObject2Map(Object obj){
        Map result  = null;
        if (obj != null && obj instanceof Map){
            result = (Map)obj;
        }
        return result;
    }


}
